home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Matrix / MyMatrix.m < prev    next >
Text File  |  1995-06-12  |  8KB  |  353 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "VarMatrix.h"
  5. #import <appkit/TextFieldCell.h>
  6. #import <objc/List.h>
  7. #import <stdlib.h>
  8.  
  9. @implementation VarMatrix
  10.  
  11. -initFrame:(NXRect *)frameRect mode:(int)aMode prototype:aCell numRows:(int)rowsHigh numCols:(int)colsWide;
  12. {
  13.     int i;
  14.     column    *thisColPtr;
  15.     row        *thisRowPtr;
  16.     
  17.     [super initFrame:frameRect mode:aMode prototype:aCell 
  18.                             numRows:rowsHigh numCols:colsWide];
  19.     
  20.     columnList = [[List alloc] init];
  21.     rowList = [[List alloc] init];
  22.     
  23.     for (i=0; i < colsWide; i++) {
  24.         thisColPtr = (column*)calloc(1,sizeof(column));
  25.         [columnList addObject:(id)thisColPtr];
  26.         thisColPtr->x = i * cellSize.width;
  27.         thisColPtr->width = cellSize.width;
  28.     }
  29.     
  30.     for (i=0; i < rowsHigh; i++) {
  31.         thisRowPtr = (row*)calloc(1,sizeof(row));
  32.         [rowList addObject:(id)thisRowPtr];
  33.         thisRowPtr->y = i * cellSize.height;
  34.         thisRowPtr->height = cellSize.height;
  35.     }
  36.     {
  37.         NXRect    rect;
  38.                                      
  39.         [self getFrame:&rect];
  40.         rect.size.width =130.;
  41.         [self setFrame:&rect];
  42.     }
  43.     return self;
  44. }
  45. -erase
  46. {
  47.     NXRect    rect;
  48.     
  49.     [self lockFocus];
  50.     [self getFrame:&rect];
  51.     NXEraseRect(&rect);
  52.     [self unlockFocus];
  53.     return self;
  54. }
  55. -setWidth:(float)width forCol:(int)col
  56. {
  57.     float    diff;
  58.     
  59.     
  60.     diff = width - ((column*)[columnList objectAt:col])->width;
  61.     ((column*)[columnList objectAt:col])->width = width;
  62.     
  63.     [self updateColsFrom:col+1 with:diff];
  64.     
  65.     
  66.     {
  67.         NXRect    rect;
  68.                                      
  69.         [self getFrame:&rect];
  70.         rect.size.width = ((column*)[columnList lastObject])->x +
  71.                                      ((column*)[columnList lastObject])->width;
  72.         [self setFrame:&rect];
  73.     }
  74.     return self;
  75. }
  76. -setHeight:(float)height forRow:(int)theRow
  77. {
  78.     float    diff;
  79.     
  80.     
  81.     diff = height - ((row*)[rowList objectAt:theRow])->height;
  82.     ((row*)[rowList objectAt:theRow])->height = height;
  83.     
  84.     [self updateRowsFrom:theRow+1 with:diff];
  85.     
  86.     {
  87.         NXRect    rect;
  88.                                      
  89.         [self getFrame:&rect];
  90.         rect.size.height = ((row*)[rowList lastObject])->y +
  91.                                      ((row*)[rowList lastObject])->height;
  92.         [self setFrame:&rect];
  93.     }
  94.     return self;
  95. }
  96. -renewRows:(int)newRows cols:(int)newCols
  97. {
  98.     column    *rectPtr;
  99.     row        *rowPtr;
  100.     int        i;
  101.     
  102.     for (i=numCols; i < newCols; i++) {
  103.         rectPtr = (column*)calloc(1,sizeof(column));
  104.         [columnList addObject:(id)rectPtr];
  105.         rectPtr->x = i * cellSize.width;
  106.         rectPtr->width = cellSize.width;
  107.     
  108.     }
  109.     for (i=numRows; i < newRows; i++) {
  110.         rowPtr = (row*)calloc(1,sizeof(row));
  111.         [rowList addObject:(id)rowPtr];
  112.         rowPtr->y = i * cellSize.height;
  113.         rowPtr->height = cellSize.height;
  114.     
  115.     }
  116.  
  117.     [super renewRows:newRows cols:newCols];
  118.     return self;
  119. }
  120. -addCol
  121. {
  122.     column    *rectPtr;
  123.     float    place;
  124.     
  125.     rectPtr = (column*)calloc(1,sizeof(column));
  126.     place = ((column*)[columnList lastObject])->x +
  127.                                      ((column*)[columnList lastObject])->width;
  128.         
  129.     [columnList addObject:(id)rectPtr];
  130.     rectPtr->x = place;
  131.     rectPtr->width = cellSize.width;
  132.     
  133.     [super addCol];
  134.     return self;
  135. }
  136. - insertColAt:(int)col
  137. {
  138.     column        *rectPtr;
  139.     float    place,diff;
  140.     
  141.     rectPtr = (column*)calloc(1,sizeof(column));
  142.     place = ((column*)[columnList objectAt:col-1])->x +
  143.                             ((column*)[columnList objectAt:col-1])->width;
  144.         
  145.     [columnList insertObject:(id)rectPtr at:col];
  146.     rectPtr->x = place;
  147.     rectPtr->width = cellSize.width;
  148.  
  149.     diff = cellSize.width;
  150.     
  151.     [self updateColsFrom:col+1 with:diff];
  152.     
  153.     return [super insertColAt:col];
  154.     
  155. }
  156. - removeColAt:(int)col andFree:(BOOL)flag
  157. {
  158.     float    diff;
  159.     
  160.     /*  is negative 'cause we want to subtract this height from the rest */
  161.     diff = -((column*)[columnList objectAt:col])->width;
  162.     [columnList removeObjectAt:col];
  163.     
  164.     [self updateColsFrom:col with: diff];
  165.     
  166.     return [super removeColAt:col andFree:flag];
  167. }
  168. -addRow
  169. {
  170.     row        *rectPtr;
  171.     float    place;
  172.     
  173.     rectPtr = (row*)calloc(1,sizeof(row));
  174.     place = ((row*)[rowList lastObject])->y +
  175.                                      ((row*)[rowList lastObject])->height;
  176.         
  177.     [rowList addObject:(id)rectPtr];
  178.     rectPtr->y = place;
  179.     rectPtr->height = cellSize.height;
  180.     
  181.     return [super addRow];
  182. }
  183. - insertRowAt:(int)theRow
  184. {
  185.     row        *rectPtr;
  186.     float    place,diff;
  187.     
  188.     rectPtr = (row*)calloc(1,sizeof(row));
  189.     place = ((row*)[rowList objectAt:theRow-1])->y +
  190.                             ((row*)[rowList objectAt:theRow-1])->height;
  191.         
  192.     [rowList insertObject:(id)rectPtr at:theRow];
  193.     rectPtr->y = place;
  194.     rectPtr->height = cellSize.height;
  195.  
  196.     diff = cellSize.width;
  197.     
  198.     [self updateRowsFrom:theRow+1 with:diff];
  199.     
  200.     return [super insertRowAt:theRow];
  201.     
  202. }
  203. - removeRowAt:(int)theRow andFree:(BOOL)flag
  204. {
  205.     float    diff;
  206.     
  207.     /*  is negative 'cause we want to subtract this height from the rest */
  208.     diff = -((row*)[rowList objectAt:theRow])->height;
  209.     [rowList removeObjectAt:theRow];
  210.     
  211.     [self updateRowsFrom:theRow with: diff];
  212.     
  213.     return [super removeRowAt:theRow andFree:flag];
  214. }
  215.  
  216. -updateRowsFrom:(int)rowNum with:(float)diff
  217. {
  218.     int        i;
  219.     for (i=rowNum; i < [rowList count] ; i++) {
  220.         ((row*)[rowList objectAt:i])->y += diff;
  221.     }
  222.     return self;
  223. }
  224. -updateColsFrom:(int)colNum with:(float)diff
  225. {
  226.     int        i;
  227.     for (i=colNum; i < [columnList count] ; i++) {
  228.         ((column*)[columnList objectAt:i])->x += diff;
  229.     }
  230.     return self;
  231. }
  232.  
  233. -update
  234. {
  235.     int    col,row;
  236.     printf("update\n");
  237.     if ([self superview] == NULL) return [super update];
  238.     [self erase];
  239.     for (col = 0; col < [columnList count]; col++) {
  240.         for (row = 0; row < [rowList count]; row++) {
  241.             [self drawCellAt:row :col];
  242.         }
  243.     }
  244.     return self;
  245. }
  246. -drawCellAt:(int)theRow :(int)col
  247. {
  248.     NXRect    cellFrame = {0.0,0.0,20.0,20.0};
  249.     printf("drawCellAt: %d :%d\n",theRow,col);
  250.     //[super drawCellAt:row :col];
  251.     cellFrame.origin.x = ((column*)[columnList objectAt:col])->x;
  252.     cellFrame.origin.y = ((row *)[rowList objectAt:theRow])->y;
  253.     cellFrame.size.width =((column*)[columnList objectAt:col])->width;
  254.     cellFrame.size.height =((row*)[rowList objectAt:theRow])->height;
  255.     [self lockFocus];
  256.     
  257.     [[self cellAt:theRow :col] drawSelf:&cellFrame inView:self];
  258.     [self unlockFocus];
  259.     return self;
  260. }
  261. -drawSelf:(const NXRect *)rects :(int)rectCount
  262. {
  263.     NXRect    rect;
  264.     
  265.     printf("drawSelf: %d\n",rectCount);
  266.     [self getFrame:&rect];
  267.     NXEraseRect(&rect);
  268.     
  269.     [super drawSelf:rects :rectCount];
  270.     return self;
  271. }
  272. -getCellFrame:(NXRect *)theRect at:(int)theRow :(int)col
  273. {
  274.     printf("getCellFrame at:%d :%d\n",theRow,col);
  275.     //[super getCellFrame:theRect at:row :col];
  276.     
  277.     NXSetRect(theRect,((column*)[columnList objectAt:col])->x,
  278.             ((row *)[rowList objectAt:theRow])->y,
  279.             ((column*)[columnList objectAt:col])->width,
  280.             ((row*)[rowList objectAt:theRow])->height);
  281.     return self;
  282. }
  283. -getRow:(int *)theRow andCol:(int*)col forPoint:(const NXPoint *)point
  284. {
  285.     int     i;
  286.     printf("getRow\n");
  287.     
  288.     *theRow = -1;
  289.     *col = -1;
  290.     
  291.     for (i = 0; i < [columnList count]; i++) {
  292.         if ((point->x >= (((column*)[columnList objectAt:i])->x)) &&
  293.             (point->x <= ((((column*)[columnList objectAt:i])->x) +
  294.                  ((column*)[columnList objectAt:i])->width))) {
  295.             *col =i;
  296.         }
  297.     }
  298.     for (i = 0; i < [rowList count]; i++) {
  299.         if ((point->y >= (((row*)[rowList objectAt:i])->y)) &&
  300.             (point->y <= ((((row*)[rowList objectAt:i])->y) +
  301.                  ((row*)[rowList objectAt:i])->height))) {
  302.             *theRow =i;
  303.         }
  304.     }
  305.     return self;
  306. }
  307. -getRow:(int *)row andCol:(int *)col ofCell:aCell
  308. {
  309.     printf("getRow for cell\n");
  310.     //[aCell getDrawRect:&rect];
  311.     //*row = abs(rect.origin.y/20.0);
  312.     //*col = abs(rect.origin.x/20.0);
  313.     [super getRow:row andCol:col ofCell:aCell];
  314.     return self;
  315. }
  316. - mouseDown:(NXEvent *)theEvent
  317. {
  318.     NXPoint        mouseDownLocation;
  319.     int            row, column;
  320.     id            activeCell;
  321.       
  322.     printf("mouseDown:\n");
  323.   /* find the cell that got clicked on and select it */
  324.     mouseDownLocation = theEvent->location;
  325.     [self convertPoint:&mouseDownLocation fromView:nil];
  326.     [self getRow:&row andCol:&column forPoint:&mouseDownLocation];
  327.     activeCell = [self cellAt:row :column];
  328.     [self selectCell:activeCell];
  329.     //[self getCellFrame:&cellFrame at:row :column];
  330.     
  331.   /* do whatever's required for a single-click */
  332.     [self sendAction];
  333.     
  334.       return self;
  335. }
  336. -write:(NXTypedStream*)typedStream
  337. {
  338.     [super write:typedStream];
  339.     NXWriteObject(typedStream,rowList);
  340.     NXWriteObject(typedStream,columnList);
  341.     return self;
  342. }
  343. -read:(NXTypedStream*)typedStream
  344. {
  345.     [super read:typedStream];
  346.     
  347.     rowList = NXReadObject(typedStream);
  348.     columnList = NXReadObject(typedStream);
  349.     return self;
  350. }
  351.  
  352. @end
  353.